package com.kmxd.ams.app.event.message.command;

import cn.hutool.core.util.ObjectUtil;
import com.kmxd.ams.client.event.message.MessageNoticeEvent;
import com.kmxd.ams.client.event.message.MessageSysEvent;
import com.kmxd.ams.client.event.message.MessageTodoEvent;
import com.kmxd.ams.client.system.ISysMessagesSvc;
import com.kmxd.ams.client.system.dto.SysMessagesAddDTO;
import com.kmxd.ams.core.common.enums.MessageSendTypeEnum;
import com.kmxd.ams.core.common.enums.MessageTypeEnum;
import com.kmxd.ams.core.util.SecurityUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发送消息实现
 *
 * @author ZuoLG
 */
@Slf4j
@Component
@AllArgsConstructor
public class SendMessageCmd {
  private final ISysMessagesSvc sysMessagesSvc;

  /** 发送待办消息 */
  @Transactional(rollbackFor = Exception.class)
  public void sendTodoMsg(MessageTodoEvent event) {
    SysMessagesAddDTO sysMessages = new SysMessagesAddDTO();
    sysMessages.setComId(event.getComId());
    sysMessages.setFondsId(SecurityUtil.getFondsId());
    sysMessages.setTitle(event.getTitle());
    sysMessages.setMsgContent(event.getMsgContent());
    if (ObjectUtil.isNotEmpty(event.getReciveUser())) {
      sysMessages.setReceiver(String.valueOf(event.getReciveUser()));
      sysMessages.setSendType(MessageSendTypeEnum.USER.getType());
    } else {
      sysMessages.setReceiver(String.valueOf(event.getReciveRole()));
      sysMessages.setSendType(MessageSendTypeEnum.ROLE.getType());
    }

    sysMessages.setMsgCategory(MessageTypeEnum.NOTICE.getType());
    sysMessages.setOpenType("components");
    sysMessages.setOpenPage(
        ObjectUtil.isNotEmpty(event.getOpenPage()) ? event.getOpenPage() : "TransferOrder");
    log.debug("docIds: {} ", sysMessages);
    sysMessagesSvc.add(sysMessages);
  }

  /** 发送系统消息 */
  @Transactional(rollbackFor = Exception.class)
  public void sendSysMsg(MessageSysEvent event) {
    SysMessagesAddDTO sysMessages = new SysMessagesAddDTO();
    sysMessages.setTitle(event.getTitle());
    sysMessages.setMsgContent(event.getMsgContent());
    if (ObjectUtil.isNotEmpty(event.getReciveUser())) {
      sysMessages.setReceiver(String.valueOf(event.getReciveUser()));
      sysMessages.setSendType(MessageSendTypeEnum.USER.getType());
    } else {
      sysMessages.setReceiver(String.valueOf(event.getReciveRole()));
      sysMessages.setSendType(MessageSendTypeEnum.ROLE.getType());
    }
    sysMessages.setMsgCategory(MessageTypeEnum.NOTICE.getType());
    sysMessages.setOpenType("components");
    sysMessages.setOpenPage(
        ObjectUtil.isNotEmpty(event.getOpenPage()) ? event.getOpenPage() : "SysMsg");
    log.debug("docIds: {} ", sysMessages);
    sysMessagesSvc.add(sysMessages);
  }

  /** 发送通知消息 */
  @Transactional(rollbackFor = Exception.class)
  public void sendNoticeMsg(MessageNoticeEvent event) {
    SysMessagesAddDTO sysMessages = new SysMessagesAddDTO();
    sysMessages.setTitle(event.getTitle());
    sysMessages.setMsgContent(event.getMsgContent());
    sysMessages.setCreateBy(event.getReciveUser());
    if (ObjectUtil.isNotEmpty(event.getReciveUser())) {
      sysMessages.setReceiver(String.valueOf(event.getReciveUser()));
      sysMessages.setSendType(MessageSendTypeEnum.USER.getType());
    } else {
      sysMessages.setReceiver(String.valueOf(event.getReciveRole()));
      sysMessages.setSendType(MessageSendTypeEnum.ROLE.getType());
    }
    sysMessages.setOpenType("components");
    sysMessages.setOpenPage(
        ObjectUtil.isNotEmpty(event.getOpenPage()) ? event.getOpenPage() : "NoticeMsg");
    log.debug("docIds: {} ", sysMessages);
    sysMessagesSvc.add(sysMessages);
  }
}
